// index.js
const app = getApp();
var funcs = require('../../static/js/funcs.js');
import {
  cities,
  areas
} from '../../static/js/city';
Page({
  data: {
    motto: '您好！',
    userInfo: {
      avatarUrl: "/static/images/avatar.png",
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    cities,
    areas,
    city: "",
    area: "",
    multiIndex: [0, 0],
    objectMultiArray: [cities, areas[0]],
    selectedmapid: "",
    loginedFlag: false
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    console.log(e)
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },

  onInputName(e) {
    const input = e.detail.value
    this.setData({
      inputName: input
    })
    console.log("onInputName>"+input)
  },

  onInputOanum(e) {
    const input = e.detail.value
    this.setData({
      inputOanum: input
    })
    console.log("onInputOanum>"+input)
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let openid = app.globalData.openid
    funcs.judgeStaff(openid).then(res=>{
      if(res.data.succeed){
        console.log("该员工已注册")
        console.log(res.data.data.priority)
        app.globalData.loginedFlag = true
        this.setData({
          loginedFlag: true,
          priority:res.data.data.priority,
          name:res.data.data.name,
          oanum:res.data.data.oanum
        })
      }else{
        app.globalData.loginedFlag = false
        this.setData({
          loginedFlag: false
        })
      }
    })
  },
  select_inst_func:function(e){
    let that = this
    console.log(this.data.networkList)
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value;
    this.setData({
      select_inst: this.data.inst_list[index],
      selectedmapid: this.data.mapid_list[index]
    })
  },

  bindColumnCityChange(event) {
    console.log('修改的列为', event.detail.column, '，值为', event.detail.value);
    switch (event.detail.column) {
      case 0: //修改第一列
        let arr = this.data.multiIndex;
        arr[0] = event.detail.value;
        arr[1] = 0;
        this.setData({
          objectMultiArray: [cities, areas[event.detail.value]],
          multiIndex: arr,
        })
        break;
      case 1: //修改第二列
        let arr2 = this.data.multiIndex;
        arr2[1] = event.detail.value;
        // console.log(arr2)
        this.setData({
          multiIndex: arr2,
        })
        break;
    }
    // console.log(this.data.multiIndex)
  },
  bindCityChange(event) {
    console.log('picker发送选择改变，携带值为', event.detail.value)
    let city_index = event.detail.value[0];
    let area_index = event.detail.value[1];
    let city = this.data.cities[city_index].name;
    let area = this.data.objectMultiArray[1][area_index].name;
    
    funcs.getAllByAreaName(city,area).then(res => {
      // console.log(res.data);
      let real_networkList = []
      let networkList = res.data.data;
      let mapidList = []
      let instList = []
      networkList.map((item, index) => {
        if (item.telephone != null && item.telephone.length > 0)
          real_networkList.push(item);
          instList.push(item.name)
          mapidList.push(item.map_id)
      })
      this.setData({
        networkList: real_networkList,
        inst_list: instList,
        mapid_list: mapidList,
        select_inst: instList.length>0? instList[0]:"全部"
      })
    }).then(() => {
      this.setData({
        city: city,
        area: area,
        city_area: city + '-' + area
      })
      console.log(this.data.city)
      console.log(this.data.area)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let openid = app.globalData.openid
    if(app.globalData.loginedFlag==false){
      this.setData({
        loginedFlag: false
      })
      return
    }
    funcs.judgeStaff(openid).then(res=>{
      if(res.data.succeed){
        console.log("该员工已注册")
        console.log(res.data.data.priority)
        app.globalData.loginedFlag = true
        this.setData({
          loginedFlag: true,
          priority:res.data.data.priority,
          name:res.data.data.name,
          oanum:res.data.data.oanum
        })
      }else{
        app.globalData.loginedFlag = false
        this.setData({
          loginedFlag: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  mgmyinfo(res){
    wx.navigateTo({
      url: '/pages/mgmyinfo/mgmyinfo'
    })
  },
  manageInst(res){
    this.errorNotice()
    return;
    wx.navigateTo({
      url: '/pages/mginst/mginst'
    })
  },

  manageGroup(res){
    wx.navigateTo({
      url: '/pages/mggroup/mggroup'
    })
  },

  manageAssess(res){
    wx.navigateTo({
      url: '/pages/mgass/mgass'
    })
  },
  manageStaff(res){
    console.log("点击管理员工")
    this.errorNotice()
    return;
    wx.navigateTo({
      url: '/pages/mgstaff/mgstaff'
    })
  },
  managePriority(res){
    this.errorNotice()
    return;
    wx.navigateTo({
      url: '/pages/managepriority/managepriority'
    })
  },
  manageMyPerson(res){
    this.errorNotice()
    return;
    console.log("点击管理客户")
    wx.navigateTo({
      url: '/pages/mgmperson/mgmperson'
    })
  },
  exitClick(res){
    wx.showToast({
      title: "因数据安全考虑\r\n目前暂不开放该功能",
      icon: 'none',
      duration: 2000
    })
  },
  submit(){
    console.log("提交绑定")
    let openid = app.globalData.openid
    let map_id = this.data.selectedmapid
    console.log(this.data.inputOanum)
    console.log(this.data.inputName)

    if(this.data.inputOanum==null||this.data.inputName==""||this.data.inputOanum==""||this.data.inputName==""||this.data.inputOanum=="undefined"||this.data.inputName=="undefined"){
      wx.showToast({
        title: '请完善信息',
        icon:"error",
        duration:2000
      })
      return;
    }
    if(!this.validateName(this.data.inputName)){
      wx.showToast({
        title: '姓名格式问题',
        icon:"error",
        duration:2000
      })
      return;
    }
    if(this.data.inputOanum.length!=6||!this.isNumeric(this.data.inputOanum)){
      wx.showToast({
        title: '工号格式问题',
        icon:"error",
        duration:2000
      })
      return;
    }
    funcs.submitStaff(openid,this.data.inputOanum,this.data.inputName,map_id,this.data.city,this.data.area).then(res=>{
      if(res.data.succeed){
        app.globalData.loginedFlag = true
        this.setData({
          loginedFlag: true,
          priority:res.data.data.priority,
          name:res.data.data.name,
          oanum:res.data.data.oanum
        })
      }else{
        app.globalData.loginedFlag = false
        this.setData({
          loginedFlag: false,
        })
        wx.showToast({
          title: '该员工已绑定',
          icon: "error",
          duration: 2000
        })
      }
    })
  },
  validateName:function (name) {
    // const regex = /^[a-zA-Z\u4e00-\u9fa5]+( [a-zA-Z\u4e00-\u9fa5]+)*$/;
    // return regex.test(name);
    // 收货人姓名校验(准则:姓名为2-8汉字)

      var regu = /^[\u4E00-\u9FA5]{2,8}$/;
      var re = new RegExp(regu);
      if (!re.test(name)) {
        return false;
      }
      return true;
  },
  isNumeric(str) {
    return !isNaN(Number(str));
  },
  errorNotice: function (res) {
    wx.showToast({
      title: "因数据安全考虑\r\n目前暂不开放该功能",
      icon: 'none',
      duration: 2000
    })
  },
})
